﻿using MediatR;
using Newtonsoft.Json;
using Warehouse.Domain;
using Warehouse.Domain.DTO;
using Warehouse.Infrstructre.Interface;
using Warehouse.Read.API.Applcation.Command.InventoryCommand;
using WMSpublic;
using static WMSpublic.ParseAPI;

namespace Warehouse.Read.API.Applcation.CommandHandlers.InventoryCommandHandler
{
    /// <summary>
    /// 显示仓库盘点中介者
    /// </summary>
    public class SelectInventoryCommandHandler : IRequestHandler<SelectInventoryCommand, InventoryPage>
    {
        private readonly IInventoryRepository _inventRep;
        private readonly IInventProductRepository _ProduceRep;

        public SelectInventoryCommandHandler(IInventoryRepository inventRep, IInventProductRepository produceRep)
        {
            _inventRep = inventRep;
            _ProduceRep = produceRep;
        }

        public async Task<InventoryPage> Handle(SelectInventoryCommand request, CancellationToken cancellationToken)
        {
            InventoryPage page = new InventoryPage();
            //List<Products> products = await GetProductsList(1, 100);
            var res = (await _inventRep.GetAllAsyns()).Where(x => x.IsDel == false && x.office_id == request.office_id);
            //var res = from invent in await _inventRep.GetAllAsyns()
            //          join product in await _ProduceRep.GetAllAsyns()
            //          on invent.InventoryId equals product.InventoryId
            //          join chan in products
            //          on product.ProductId equals chan.ProductsID
            //          where invent.IsDel == false && invent.office_id == request.office_id && chan.ProductsID == product.ProductId
            //          select new InventoryDTO
            //          {
            //              InventoryId = invent.InventoryId,
            //              InventoryCode = invent.InventoryCode,
            //              MorderNumber = invent.MorderNumber,
            //              MakePerson = invent.MakePerson,
            //              MarkTime = invent.MarkTime,
            //              IRremark = invent.IRremark,
            //              InventoryState = invent.InventoryState,
            //              InventoryType = invent.InventoryType,
            //              office_id = invent.office_id,
            //              user_id = invent.user_id,
            //              ProductsID = chan.ProductsID,
            //              AddTime = invent.AddTime
            //          };



            if (!string.IsNullOrEmpty(request.InventoryCode))
            {
                res = res.Where(x => x.InventoryCode == request.InventoryCode);
            }
            if (!string.IsNullOrEmpty(request.Kdate))
            {
                res = res.Where(x => x.AddTime >= DateTime.Parse(request.Kdate));
            }
            if (request.InventoryState > 0)
            {
                res = res.Where(x => x.InventoryState == request.InventoryState);
            }
            page.SumCount = res.Count();
            page.PageLast = (int)Math.Ceiling(page.SumCount * 1.0 / request.PageSize);
            page.list = res.Skip((request.PageIndex - 1)).Take(request.PageSize).ToList();
            return page;
        }



        /// <summary>
        /// 产品
        /// </summary>
        /// <returns></returns>
        public async Task<dynamic> GetProductsList(int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIzIiwic2lkIjoiMTg1OTgwMzUwNjYzMDg1NjcwNCIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIxLDEsMSwxIiwianRpIjoiNDMzMDY2ZGYtZjI2MC00MjY3LWJhZjktYmQzODFkZWFkYzBiIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6IuWNouiKsyIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3VyaSI6Ii8iLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.sWP96tbSYf5jz3reVpvWkhrkHgM2U5YkHFmwk4E7_V0";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/cpd/api/ProductRead/GetAllProductInfo?pageindex=" + pindex + "&pagesize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Products>>(postReponse);
            return json.data.list;
        }

        //产品
        public class Products
        {
            /// <summary>
            /// 产品id
            /// </summary>
            public int ProductsID { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            public string? ProductsCode { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            public string? ProductsName { get; set; }
            /// <summary>
            /// 厂商编码
            /// </summary>
            public string? VendorsCode { get; set; }
            /// <summary>
            /// 内部编码
            /// </summary>
            public string? InteriorCode { get; set; }
            /// <summary>
            /// 规格
            /// </summary>
            public string? Specs { get; set; }
            /// <summary>
            /// 产品类别
            /// </summary>
            public int? ProductTypeID { get; set; }
            /// <summary>
            /// 存储单位
            /// </summary>
            public string? StorageUnits { get; set; }
            /// <summary>
            /// 预警（上）
            /// </summary>
            public int? Upperlimit { get; set; }
            /// <summary>
            /// 预警（下）
            /// </summary>
            public int? Lowerlimit { get; set; }
            /// <summary>
            /// 包装类型
            /// </summary>
            public int? WrapType { get; set; }
            /// <summary>
            /// 价格
            /// </summary>
            public decimal? Price { get; set; }
            /// <summary>
            /// 重量
            /// </summary>
            public decimal? Weight { get; set; }
            /// <summary>
            /// 默认仓库
            /// </summary>
            public int? WarehouseId { get; set; }
            /// <summary>
            /// 库位
            /// </summary>
            public int? StorehouseId { get; set; }
            /// <summary>
            /// 默认供应商
            /// </summary>
            public int? VendorId { get; set; }
            /// <summary>
            /// 默认客户
            /// </summary>
            public int? ClientId { get; set; }
            /// <summary>
            /// 描述
            /// </summary>
            public string? Describe { get; set; }
            /// <summary>
            /// 产品数量
            /// </summary>
            public int? ProcurementNum { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? Office_Id { get; set; }

            /// <summary>
            /// 用户id
            /// </summary>
            public int? User_Id { get; set; }
        }






    }
}
